/*
 * ToggleButtonDelegate.cpp  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ToggleButtonDelegate.h"

// ToggleButtonDelegate :: Protected Constructors. -----------------------------

/*!
 * \brief Constructs new ToggleButtonDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the ToggleButton java object
 * \param button the button the QAbstractButton object
 */
ToggleButtonDelegate::ToggleButtonDelegate(JNIEnv *env, jobject obj,
                                           QAbstractButton *button)
  : AbstractButtonDelegate(env, obj, button)
{
  // empty constructor body
}

/*!
 * \brief Constructs new ToggleButtonDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the ToggleButton java object
 */
ToggleButtonDelegate::ToggleButtonDelegate(JNIEnv *env, jobject obj)
  : AbstractButtonDelegate(env, obj, new QPushButton())
{
  qToggleButton()->setCheckable(true);
}

// ToggleButtonDelegate :: Native Calls. --------------------------------------

/*!
 * \brief Selectes/unselects the button.
 *
 * \param env the JNIEnv pointer 
 * \param selected \c true if selected
 */
void ToggleButtonDelegate::setSelected(JNIEnv *env, jboolean selected)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setSelected, SWING_ENV_ARG, JBOOLEAN_ARG(selected))
  qAbstractButton()->setChecked(selected);
  if (qAbstractButton()->isChecked()) {
    setIconType(QIconAdapter::SelectedIcon);
  } else {
    setIconType(QIconAdapter::DefaultIcon);
  }
}

// ToggleButtonDelegate :: Static Native Calls. --------------------------------

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong ToggleButtonDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new ToggleButtonDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}
